<?php

namespace App\Http\Controllers\Wechat;




use App\Model\Marketing\Post;
use App\Model\Wechat\WechatSubscribe;
use Illuminate\Http\Request;
use App\Model\System\Token;


class MessageController extends HelpController
{


    /**
     * 群发图文客服消息
     * @return \EasyWeChat\Support\Collection
     *
     */
    public function sendNews(Request $request)
    {
        try {
            $data = null;
            if ($request->has(['title', 'url', 'image','id'])) {
                $data[0] = $request;
            }
            if ($request->has('data')) {
                $data = $request->get('data');
            }
            if ($data) {
                $this->sendtoWechat($data);
                $this->updatestatus($data);
                return $this->success(true,'分享成功');
            } else {
                return $this->fail(false, '请求数据不全');
            }
        } catch (\Exception $e) {
            return $this->fail('发送失败' . $e->getMessage());
        }
    }

    public function getSubscribe()
    {
        $data = WechatSubscribe::find(1);
        if($data){
            return $this->success($data);
        }else{
            return $this->fail('无数据');
        }
    }


    public function setSubscribe(Request $request)
    {
        $obj = WechatSubscribe::find(1);
        if(!$obj){
            $fields = $request->all();
            $obj = new WechatSubscribe($fields);
            $obj->id = 1;
            $obj->created_by = Token::user()->userid;
            $obj->updated_by = Token::user()->userid;
            if($obj->save()){
                return $this->success($obj,"创建成功");
            }else{
                return $this->fail(false,'创建失败');
            }
        }else{
            $obj->update($request->all());
            $obj->updated_by = Token::user()->userid;
            if($obj->save()){
                return $this->success($obj,"修改成功");
            }else{
                return $this->fail(false,'修改失败');
            }
            return $this->fail('已添加过订阅消息');
        }
    }


    /**
     * 群发图文消息
     * @param Request $request
     * @return array
     */
    protected function sendtoWechat($data)
    {

        $messages = [];
        foreach ($data as $item){
            $message = $this->newsMessage($item);
            $messages[] = $message;
        }
        $list = $this->getUserList();
        foreach ($list as $user){
            try{
                $result = $this->app->staff->message($messages)->to($user)->send();
            }catch (\Exception $e){
                continue;
            }
        }
    }

    /**
     * 更新营销发布状态
     * @param Request $request
     * @return array
     */
    protected function updatestatus($data)
    {
        foreach ($data as $item){
            $post = Post::find($item['id']);
            $post->status = 2;
            $post->save();
        }
    }

    /**
     * 获取用户列表
     * @return \EasyWeChat\Support\Collection
     *
     */
    protected function getUserList()
    {
        try{
            $userService  = $this->app->user;
            $result = $userService->lists();
            $list = $result['data']['openid'];
            return $list;
        }catch (\Exception $e){
            return $this->fail('获取失败'.$e->getMessage());
        }

    }


    /**
     * 获取模板id
     * @param Request $request
     * @return array
     */

    protected function getTemplate()
    {
        try {
            $notice = $this->app->notice;
            $result = $notice->getPrivateTemplates();
            $templateid = $result['template_list'][0]['template_id'];
            return $templateid;
        } catch (\Exception $e) {
            throw $e;
        }
    }


}


//
//    /**
//     * 群发图文消息
//     * @param Request $request
//     * @return array
//     */
//
//    protected function sendNews($id=null,$url)
//    {
//        try{
//            $post = Post::find($id);
//            $cover = trim($post->cover,'/');
//            $thumb = Eeofile::getLocalPath($cover);
//            $thumb = 'C:/Users/EEO/Pictures/217340731752917918.jpg';
//            $thumb_media_id = $this->uploadThumb($thumb);
//            $articleid = $this->uploadArticle($post,$thumb_media_id,$url);
//
//            $wechat_post = new WechatPost();
//            $wechat_post->postid = $id;
//            $wechat_post->mediaid = $articleid;
//            $wechat_post->save();
//
//            $this->app->broadcast->sendNews($articleid);
//        }catch (\Exception $e){
//            throw $e;
//        }
//    }
//
//    protected function uploadThumb($path)
//    {
//
//        // 永久素材
//        $material = $this->app->material;
//
//        $result = $material->uploadThumb($path);
//        $thumb_media_id = $result->media_id;
//        return $thumb_media_id;
//    }
//
//    protected function uploadArticle($post,$mediaid,$url)
//    {
//        // 上传单篇图文
//        $article = new Article([
//            'title' => $post->title,
//            'thumb_media_id' => $mediaid,
//            'author' => null,
//            'digest' => null,
//            'show_cover_pic' => 1,
//            'content' => $post->body,
//            'content_source_url' => $url,
//            //...
//        ]);
//        // 永久素材
//        $material = $this->app->material;
//
//        $result = $material->uploadArticle($article);
//        $mediaId = $result->media_id;
//        return $mediaId;
//    }



